﻿using System;
using System.Security.Permissions;
using System.Timers;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Newtonsoft.Json;

namespace 疫情数据
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>

    public partial class MainWindow : Window
    {
        
        static int RGBvalue = 50;
        static int RGBDelay = 10;

        Timer RGBTimer = new Timer();        //定义RGB事件时钟

        Timer DataTimer = new Timer();       //定义Json事件时钟

        public static void Delay(double ms)  //不假死的延时函数
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(ms) > DateTime.Now)
            {
                DispatcherHelper.DoEvents();
            }
            return;
        }

        public static class DispatcherHelper //延时函数主体方法
        {
            [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
            public static void DoEvents()
            {
                DispatcherFrame frame = new DispatcherFrame();
                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
                try { Dispatcher.PushFrame(frame); }
                catch (InvalidOperationException) { }
            }
            private static object ExitFrames(object frame)
            {
                ((DispatcherFrame)frame).Continue = false;
                return null;
            }
        }

        public MainWindow()                  //主窗口的入口方法
        {

            InitializeComponent();

            RGBTimer.Elapsed += new ElapsedEventHandler(TittleRGB);  //初始化时间周期事件
            RGBTimer.Interval = 10;                                  //设置时钟时间
            RGBTimer.AutoReset = true;                               //设置时钟循环执行为真
            RGBTimer.Enabled = true;                                 //设置时钟开始计时

            DataTimer.Elapsed += new ElapsedEventHandler(DataTime);  //初始化时间周期事件
            DataTimer.Interval = 120000;                             //设置时钟时间
            DataTimer.AutoReset = true;                              //设置时钟循环执行为真
            DataTimer.Enabled = true;                                //设置时钟开始计时

            JsonUpdata();

            WindowCenter();

            Height = 90;

            Head.Opacity = 0;

            Global_Data.Opacity = 0;
            China_Data.Opacity = 0;
            High_Data.Opacity = 0;
            Local_Data.Opacity = 0;

            Global_Data_Eff.Opacity = 0;
            China_Data_Eff.Opacity = 0;
            High_Data_Eff.Opacity = 0;
            Local_Data_Eff.Opacity = 0;

            this.Visibility = Visibility.Visible;

            WindowStart(4, 870, 30,1);

            GirdEff_Start();

        }

        public void DataTime(object source, ElapsedEventArgs e)                     //    Json数据更新时钟事件
        {

            JsonUpdata();

        }

        private void Exit_Button_MouseEnter(object sender, MouseEventArgs e)        //退出程序按钮鼠标移入事件
        {

            while (ExitButton_Eff.Opacity < 0.7) 
            {

                ExitButton_Eff.Opacity += 0.01;
                Delay(2);

            }

            //BitmapImage ima = new BitmapImage(new Uri("Img/Exit_2.png", UriKind.Relative));
            //Exit_Button.Source = ima;

        }

        private void Exit_Button_MouseLeave(object sender, MouseEventArgs e)        //退出程序按钮鼠标离开事件
        {
            while (ExitButton_Eff.Opacity > 0)
            {

                ExitButton_Eff.Opacity -= 0.01;
                Delay(2);

            }

            //BitmapImage ima = new BitmapImage(new Uri("Img/Exit_1.png", UriKind.Relative));
            //Exit_Button.Source = ima;

        }

        private void Exit_Button_MouseUp(object sender, MouseButtonEventArgs e)     //退出程序按钮鼠标按下事件
        {

            RGBTimer.Stop();
            DataTimer.Stop();

            while (Exit_Button.Opacity > 0) 
            {

                Exit_Button.Opacity -= 0.02;
                Delay(1);

            }

            Delay(100);

            Global_Data.Opacity = 1;
            China_Data.Opacity = 1;
            High_Data.Opacity = 1;
            Local_Data.Opacity = 1;

            GirdEff_End();

            WindowEnd(4,30,1);

            this.Close();

        }

        private void HeadTittle_MouseDown(object sender, MouseButtonEventArgs e)    //头部标题鼠标按下拖动事件
        {
            HeadTittle.Cursor = Cursors.SizeAll;

            Head.Width = 760;

            this.DragMove();

            Head.Width = 770;

            HeadTittle.Cursor = Cursors.Arrow;

        }

        public void TittleRGB(object source, ElapsedEventArgs e)                    // 窗口标题RGB循环发光算法
        {
            RGBTimer.Enabled = false;

            int r = 255;
            int g = 0;
            int b = 0;

            for (g = RGBvalue; g < 255; g++)
            {
                RGBUpload(r, g, b);
                System.Threading.Thread.Sleep(RGBDelay);
            }
            for (; r > RGBvalue; r--)
            {
                RGBUpload(r, g, b);
                System.Threading.Thread.Sleep(RGBDelay);
            }
            for (b = RGBvalue; b < 255; b++)
            {
                RGBUpload(r, g, b);
                System.Threading.Thread.Sleep(RGBDelay);
            }
            for (; g > RGBvalue; g--)
            {
                RGBUpload(r, g, b);
                System.Threading.Thread.Sleep(RGBDelay);
            }
            for (r = RGBvalue; r < 255; r++)
            {
                RGBUpload(r, g, b);
                System.Threading.Thread.Sleep(RGBDelay);
            }
            for (; b > RGBvalue; b--)
            {
                RGBUpload(r, g, b);
                System.Threading.Thread.Sleep(RGBDelay);
            }

            RGBTimer.Enabled = true;

        }                

        public void RGBUpload(int r, int g, int b)                                  // RBG值跨线程传送至主窗口
        {

            this.TittleDrop.Dispatcher.Invoke(        //必须从此方法跨越线程调整窗体控件，否则无法控制
                new Action(
                    delegate
                    {
                        TittleDrop.Color = Color.FromRgb((byte)r, (byte)g, (byte)b);
                    }
                )
            );

        }

        public void GirdEff_Start()                                                 //四个数据分块淡入效果事件
        {

            while (Global_Data.Opacity < 1)
            {
                Global_Data.Opacity += 0.01;
                Global_Data_Eff.Opacity += 0.006;
                Delay(2);
            }

            while (China_Data.Opacity < 1)
            {
                China_Data.Opacity += 0.01;
                China_Data_Eff.Opacity += 0.006;
                Delay(2);
            }

            while (High_Data.Opacity < 1)
            {
                High_Data.Opacity += 0.01;
                High_Data_Eff.Opacity += 0.006;
                Delay(2);
            }

            while (Local_Data.Opacity < 1)
            {
                Local_Data.Opacity += 0.01;
                Local_Data_Eff.Opacity += 0.006;
                Delay(2);
            }

        }

        public void GirdEff_End()                                                   //四个数据分块淡出效果事件
        {

            while (BodeGird.Opacity > 0)
            {
                BodeGird.Opacity -= 0.01;

                Delay(3);
            }

            Delay(200);

        }

        public void WindowStart(int size_top, int height_win, int size, int time)   //窗口创建完毕后的载入动画
        {

            int top_size = (int)Top;
            WindowCenter();

            Delay(100);

            while (Head.Opacity < 1)
            {
                Head.Opacity += 0.01;
                Delay(5);
            }

            Delay(100);

            while (Top > top_size) 
            {
                Top = Top - size_top;
                Delay(time);
            }

            Delay(100);

            while (Height < height_win) 
            {
                Height += size;
                Delay(time);
            }

            Height = 870;

        }

        public void WindowEnd(int size_top, int size, int time)                     //窗口准备结束时的动画效果
        {

            while(Opacity > 0) 
            {

                Opacity -= 0.01;
                Delay(3);
            
            }

        }

        public void WindowCenter()                                                  //        设置窗口居中效果
        {
            Top = (SystemParameters.PrimaryScreenHeight - Height) / 2;
            Left = (SystemParameters.PrimaryScreenWidth - Width) / 2;
        }

        public void JsonUpdata()                                                    //  Json数据更新操作主方法
        {

            string url = "https://m.sm.cn/api/rest?format=json&method=Huoshenshan.healingLocal";
            string Data = HttpUitls.Get(url);

            Root src = JsonConvert.DeserializeObject<Root>(Data);

            if (src.cityData.die_cnt == "-") src.cityData.die_cnt = "0";
            if (src.provinceData.die_cnt == "-") src.provinceData.die_cnt = "0";

            Global_new.Content = src.foreign.info.sure_new_cnt;
            Global_now.Content = src.foreign.info.present;
            Global_total.Content = src.foreign.info.sure_cnt;
            Global_cure.Content = src.foreign.info.cure_cnt;
            Global_die.Content = src.foreign.info.die_cnt;


            China_new.Content = src.contryData.sure_cnt;
            China_new_new.Content = "+" + src.contryData.yst_cure_cnt;
            China_input.Content = src.contryData.input_cnt;
            China_input_new.Content = "+" + src.contryData.input_cnt_incr;
            China_hid.Content = src.contryData.hidden_cnt;
            China_hid_new.Content = "+" + src.contryData.hidden_cnt_incr;
            China_cure.Content = src.contryData.cure_cnt;
            China_cure_new.Content = "+" + src.contryData.yst_cure_cnt;
            China_die.Content = src.contryData.die_cnt;
            China_die_new.Content = "+" + src.contryData.yst_die_cnt;


            Country_one.Content = src.foreign.list[0].country;
            Country_two.Content = src.foreign.list[1].country;
            Country_three.Content = src.foreign.list[2].country;

            Country_one_now.Content = src.foreign.list[0].present;
            Country_two_now.Content = src.foreign.list[1].present;
            Country_three_now.Content = src.foreign.list[2].present;

            Country_one_total.Content = src.foreign.list[0].sure_cnt;
            Country_one_total_new.Content = "+" + src.foreign.list[0].sure_new_cnt;
            Country_two_total.Content = src.foreign.list[1].sure_cnt;
            Country_two_total_new.Content = "+" + src.foreign.list[1].sure_new_cnt;
            Country_three_total.Content = src.foreign.list[2].sure_cnt;
            Country_three_total_new.Content = "+" + src.foreign.list[2].sure_new_cnt;

            Country_one_cure.Content = src.foreign.list[0].cure_cnt;
            Country_two_cure.Content = src.foreign.list[1].cure_cnt;
            Country_three_cure.Content = src.foreign.list[2].cure_cnt;

            Country_one_die.Content = src.foreign.list[0].die_cnt;
            Country_two_die.Content = src.foreign.list[1].die_cnt;
            Country_three_die.Content = src.foreign.list[2].die_cnt;

            Country_one_dies.Content = ((double.Parse(src.foreign.list[0].die_cnt) / double.Parse(src.foreign.list[0].sure_cnt)) * 100).ToString("0.00") + "%";
            Country_two_dies.Content = ((double.Parse(src.foreign.list[1].die_cnt) / double.Parse(src.foreign.list[1].sure_cnt)) * 100).ToString("0.00") + "%";
            Country_three_dies.Content = ((double.Parse(src.foreign.list[2].die_cnt) / double.Parse(src.foreign.list[2].sure_cnt)) * 100).ToString("0.00") + "%";

            Country_one_10w.Content = ((double.Parse(src.foreign.list[0].sure_cnt) / double.Parse(src.foreign.list[0].population)) * 100000).ToString("0");
            Country_two_10w.Content = ((double.Parse(src.foreign.list[1].sure_cnt) / double.Parse(src.foreign.list[1].population)) * 100000).ToString("0");
            Country_three_10w.Content = ((double.Parse(src.foreign.list[2].sure_cnt) / double.Parse(src.foreign.list[2].population)) * 100000).ToString("0");

            Country_one_people.Content = (double.Parse(src.foreign.list[0].population) / 10000).ToString("0") + "万";
            Country_two_people.Content = (double.Parse(src.foreign.list[1].population) / 10000).ToString("0") + "万";
            Country_three_people.Content = (double.Parse(src.foreign.list[2].population) / 10000).ToString("0") + "万";


            Local_one.Content = src.cityData.province;
            Local_two.Content = src.cityData.city;

            Local_one_now.Content = src.provinceData.present;
            Local_two_now.Content = src.cityData.present;

            Local_one_total.Content = src.provinceData.sure_cnt;
            Local_one_total_new.Content = "+" + src.provinceData.sure_new_cnt;
            Local_two_total.Content = src.cityData.sure_cnt;
            Local_two_total_new.Content = "+" + src.cityData.sure_new_cnt;

            Local_one_cure.Content = src.provinceData.cure_cnt;
            Local_two_cure.Content = src.cityData.cure_cnt;

            Local_one_die.Content = src.provinceData.die_cnt;
            Local_two_die.Content = src.cityData.die_cnt;

            Local_one_dies.Content = ((double.Parse(src.provinceData.die_cnt) / double.Parse(src.provinceData.sure_cnt)) * 100).ToString("0.00") + "%";
            Local_two_dies.Content = ((double.Parse(src.cityData.die_cnt) / double.Parse(src.cityData.sure_cnt)) * 100).ToString("0.00") + "%";

            Local_one_cures.Content = ((double.Parse(src.provinceData.cure_cnt) / double.Parse(src.provinceData.sure_cnt)) * 100).ToString("0.00") + "%";
            Local_two_cures.Content = ((double.Parse(src.cityData.cure_cnt) / double.Parse(src.cityData.sure_cnt)) * 100).ToString("0.00") + "%";

            Local_one_unup.Content = src.provinceData.sure_nzd_cnt;
            Local_two_unup.Content = src.cityData.sure_nzd_cnt;

        }

    }
}
